package cn.jekun.web.shop.service;

import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ibatis.sqlmap.engine.mapping.sql.Sql;

import cn.jekun.web.shop.bean.ShopDelivery;
import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.NotNull;
import cn.jekun.component.base.validator.annotation.Num;
import cn.jekun.component.base.validator.annotation.Security;
import cn.jekun.component.core.StringUtil;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.ErpShopDelivery;
import cn.jekun.component.core.bean.gen.ErpShopImage;
import cn.jekun.component.core.bean.gen.LsLog;
import cn.jekun.component.core.bean.gen.RsUser;
import cn.jekun.component.core.bean.gen.RsUserShop;
import cn.jekun.component.core.service.ComponentCoreService;
import cn.jekun.component.core.service.gen.ErpShopDeliveryService;
import cn.jekun.component.core.service.gen.ErpShopImageService;
import cn.jekun.component.core.service.gen.ErpShopService;
import cn.jekun.component.core.service.gen.LsLogService;
import cn.jekun.component.core.service.gen.RsUserService;
import cn.jekun.web.shop.bean.Shop;
import cn.jekun.web.shop.bean.UserShop;
/**
 * 商户
 * @author twj
 *
 */
@Service(ShopService.BEAN_NAME)
public class ShopService extends ComponentCoreService{
	public static final String BEAN_NAME="cn.jekun.web.shop.service.ShopService";
	public static final String NAMESPACE="ACTION_ERP_SHOP";
	@Override
	public String getNamespace() {
		return NAMESPACE;
	}
	@Resource(name=RsUserService.BEAN_NAME)
	RsUserService userService;
    
	@Resource(name=ErpShopImageService.BEAN_NAME)
	ErpShopImageService erpShopImageService;
	
	@Resource(name=ErpShopService.BEAN_NAME)
	ErpShopService erpShopService;
	
	@Resource(name=LsLogService.BEAN_NAME)
	LsLogService lsLogService;
	
	@Resource(name=ErpShopDeliveryService.BEAN_NAME)
	ErpShopDeliveryService erpShopDeliveryService;
	
	
	/**
	 * 根据商家ID查询商家详情
	 * @param shopId
	 * @return
	 */
	public ErpShop one(@ErpShop.AnnoShopId String shopId){
		Map formMap=new HashMap<String, String>();
		formMap.put(Shop.COL_SHOP_ID, shopId);
		SqlUtis.prepareSql(formMap
				, SqlUtis.getSQL(formMap,"a."+Shop.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ)
				);
		return this.one(this.getStatementName("list"),formMap);
	}
	
	/**
	 * 根据商家ID查询商家详情
	 * @param shopId
	 * @return
	 */
	public Shop getShopDetial(@Shop.AnnoShopId String shopId){
		Map formMap=new HashMap<String, String>();
		formMap.put(Shop.COL_SHOP_ID, shopId);
		SqlUtis.prepareSql(formMap
				, SqlUtis.getSQL(formMap,"a."+Shop.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ)
				);
		return this.one(formMap);
	}
	
	/**
	 * 更新商家信息
	 * @return
	 */
	@Transactional
	public void updateShop(@ErpShop.AnnoShopId String shopId,@ErpShop.AnnoShopName String shopName,
						  @ErpShop.AnnoIntroduce String introduce,
						  @ErpShop.AnnoComName String comName,@ErpShop.AnnoShopTypeId String shopTypeId,
						  @ErpShop.AnnoAgentId String agentId,@ErpShop.AnnoProvinceId String provinceId,
						  @ErpShop.AnnoCityId String cityId,@ErpShop.AnnoRegionId String regionId,
						  @ErpShop.AnnoStreet String street,@ErpShop.AnnoAddress String address,
						  @ErpShop.AnnoContact String contact,@ErpShop.AnnoBusinessHours String businessHours,
						  @ErpShop.AnnoMinAmount String minAmount,@Num(length=5) String range,
						  @Num(length=5) String shipTime,@ErpShop.AnnoPhone String phone,
						  @ErpShop.AnnoMobile String mobile,@ErpShop.AnnoLogoUrl String logoUrl,
						  @ErpShop.AnnoLongitude String longitude,@ErpShop.AnnoLatitude String latitude,
					  	  @Num(length=5) String mcc,@ErpShop.AnnoMemo String memo,
					  	  @ErpShop.AnnoBusinessStatusType String businessStatusType,
					  	  @ErpShop.AnnoBusinessStatus String businessStatus,@ErpShop.AnnoMsgMobile String msgMobile,
					  	  @ErpShop.AnnoDeliveryAmount String deliveryAmount,
					  	  @Security String shopDeliveryId
							){
//		@ErpShop.AnnoLogoUrl String logoUrl
		ErpShop shop=new ErpShop();
		shop.setShopName(shopName);
		shop.setIntroduce(introduce);
		shop.setComName(comName);
		shop.setShopTypeId(shopTypeId);
		shop.setAgentId(agentId);
		shop.setProvinceId(provinceId);
		shop.setCityId(cityId);
		shop.setRegionId(regionId);
		shop.setStreet(street);;
		shop.setAddress(address);
		shop.setContact(contact);
		shop.setBusinessHours(businessHours);
		shop.setMsgMobile(msgMobile);
		if(!StringUtils.isBlank(deliveryAmount)){
			shop.setDeliveryAmount(Double.parseDouble(deliveryAmount));
		}else{
			shop.setDeliveryAmount(0.0);
		}
		if(minAmount !=null && !"".equals(minAmount)){
		shop.setMinAmount(Double.parseDouble(minAmount));
		}
		if(range != null && !"".equals(range)){
		shop.setRange(Integer.parseInt(range));
		}
		if(shipTime != null && !"".equals(shipTime)){
		shop.setShipTime(Integer.parseInt(shipTime));
		}
		shop.setPhone(phone);
		shop.setMobile(mobile);
		shop.setLogoUrl(logoUrl);
		shop.setLongitude(Double.parseDouble(longitude));
		shop.setLatitude(Double.parseDouble(latitude));
//		shop.setMcc(mcc);
		shop.setMemo(memo);
		shop.setBusinessStatus(businessStatus);
		shop.setBusinessStatusType(businessStatusType);
		Map map = new HashMap();
		map.put("erpShop", shop);
		map.put(ErpShop.COL_SHOP_ID,shopId);
		SqlUtis.prepareSql(map, "",
				SqlUtis.getSQLRequired(map, "a."+ErpShop.COL_SHOP_ID, Types.VARCHAR));
		this.update(map);
		
		//先删除商家配送方式
		Map infoMap = new HashMap();
		infoMap.put(ErpShopDelivery.COL_SHOP_ID, shopId);
		SqlUtis.prepareSql(infoMap, 
				SqlUtis.getSQL(infoMap, ErpShopDelivery.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ));
		this.erpShopDeliveryService.delete(infoMap);
		
		//添加新支付类型
		infoMap.clear();
		
		String[] shopDeliveryIdArray=shopDeliveryId.split(",");
		
		for(int i=0;i<shopDeliveryIdArray.length;i++){
			
			if(!StringUtils.isBlank(shopDeliveryIdArray[i])){

				ErpShopDelivery erpShopDelivery = new ErpShopDelivery();
				erpShopDelivery.setShopId(shopId);
				erpShopDelivery.setDeliveryId(shopDeliveryIdArray[i]);
				infoMap.put("erpShopDelivery", erpShopDelivery);
				this.erpShopDeliveryService.insert(infoMap);
				
			}
			
		}
	}
	/**
	 * 查询用户，并用户不能为店员
	 * @param page
	 * @return
	 */
	public List<RsUser> queryAllUser(@RsUser.AnnoUserId String userId,@RsUser.AnnoUserName String userName,@NotNull(message="分页对象不为空")Page page){
		Map formMap = new HashMap();
		String sql="";
		if(userId !=null && !"".equals(userId)){ //不为空进行模糊查询
			//" and t.TERMINAL_ID like '%"+terminalId.toString().trim()+"%'"
			sql +="and a.user_id like '%"+userId.toString().trim() +"%'";
		}
		if(userName !=null && !"".equals(userName)){
			sql +="and a.user_name like '%"+ userName.toString().trim() +"%'";
		}
		sql +="and not exists(select 1 from rs_user_shop b where a.user_id = b.user_id)";
		SqlUtis.prepareSql(formMap, sql);
		return this.userService.list(formMap, page);
	}
	
	/**
	 * 修改营业状态
	 * @param shopId
	 * @param businessStatus
	 * @return
	 */
	@Transactional
	public int updateBusinessStatus(@ErpShop.AnnoShopId String shopId,@ErpShop.AnnoBusinessStatus String businessStatus){
		//@ErpShop.AnnoLogoUrl String logoUrl
		ErpShop shop=new ErpShop();
		shop.setBusinessStatus(businessStatus);
		Map map = new HashMap();
		map.put("erpShop", shop);
		map.put(ErpShop.COL_SHOP_ID,shopId);
		SqlUtis.prepareSql(map, "",
				SqlUtis.getSQLRequired(map, "a."+ErpShop.COL_SHOP_ID, Types.VARCHAR));
		
		return this.update(this.getStatementName("updateBusinessStatus"),map);
	}
	
	
	/**
	 * 获取商家相册相片
	 * @param shopId
	 * @return
	 */
	public List<ErpShopImage> queryShopPhotoList(@ErpShopImage.AnnoShopId String shopId){
		Map formMap = new HashMap();
		String sql="";
		if(shopId !=null && !"".equals(shopId)){ //不为空进行模糊查询
			//" and t.TERMINAL_ID like '%"+terminalId.toString().trim()+"%'"
			sql +="and a.shop_id = '"+shopId.trim() +"'";
		}
		
		SqlUtis.prepareSql(formMap, sql);
		return this.erpShopImageService.list(formMap);
	}
	
	
	
	/**
	 * 添加商家相册相片
	 * @param shopId
	 * @param sort
	 * @param imageUrl
	 */
	public void addShopPhotos(@ErpShopImage.AnnoShopId String shopId,@Num(length=2)String sort,@ErpShopImage.AnnoImageUrl String imageUrl){
		
		ErpShopImage erpShopImage=new ErpShopImage();
		erpShopImage.setShopId(shopId);
		erpShopImage.setSort(Integer.parseInt(sort));
		erpShopImage.setImageUrl(imageUrl);
		
		this.erpShopImageService.insert(erpShopImage);
	}
	
	
	/**
	 * 检测该位置是否已经存在照片
	 * @param shopId
	 * @param sort
	 * @param imageUrl
	 */
	public boolean exitsPhotoBysort(@ErpShopImage.AnnoShopId String shopId,@Num(length=2)String sort){
		
		Map formMap = new HashMap();
		formMap.put(ErpShopImage.COL_SHOP_ID, shopId);
		formMap.put(ErpShopImage.COL_SORT, sort);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap,"a."+ErpShopImage.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),
				SqlUtis.getSQLRequired(formMap,"a."+ErpShopImage.COL_SORT,Types.VARCHAR,SqlUtis.EQ));
		return this.erpShopImageService.exists(formMap);
	}
	
	
	
	/**
	 * 更新商家相册相片
	 * @param shopId
	 * @param sort
	 * @param imageUrl
	 */
	public int updateShopPhotos(@ErpShopImage.AnnoShopId String shopId,@ErpShopImage.AnnoImageId String imageId,@Num(length=2)String sort,@ErpShopImage.AnnoImageUrl String imageUrl){
		
		
		ErpShopImage erpShopImage=new ErpShopImage();
		erpShopImage.setShopId(shopId);
		erpShopImage.setSort(Integer.parseInt(sort));
		erpShopImage.setImageUrl(imageUrl);
		erpShopImage.setImageId(imageId);
		
//		Map map = new HashMap();
//		map.put("ErpShopImage", erpShopImage);
//		map.put(ErpShopImage.COL_IMAGE_ID,imageId);
//		SqlUtis.prepareSql(map, "",
//				SqlUtis.getSQLRequired(map, "a."+ErpShopImage.COL_IMAGE_ID, Types.VARCHAR));
		
		return this.erpShopImageService.update(erpShopImage);
	}
	
	/**
	 * 删除商家相册相片
	 * @param imageId
	 */
	public int deleteShopPhotos(@ErpShopImage.AnnoImageId String imageId){
		
		return this.erpShopImageService.delete(imageId);
	}
	/**
	 * update商家网址
	 * @param shopId
	 * @param secondDomain
	 * @return
	 */
	public int updateDomain(@ErpShop.AnnoShopId String shopId,@ErpShop.AnnoUrl String secondDomain){
		ErpShop erpShop = new ErpShop();
		erpShop.setShopId(shopId);
		erpShop.setUrl(secondDomain);
		return this.erpShopService.update(erpShop);
	}
	/**
	 * 检查二级域名是否被注册过
	 * @param secondDomain
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public boolean isReg(@ErpShop.AnnoShopId String shopId,@ErpShop.AnnoUrl String secondDomain){
		Map formMap = new HashMap();
		formMap.put(ErpShop.COL_URL, secondDomain);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap,"a."+ErpShop.COL_URL,Types.VARCHAR,SqlUtis.EQ));
		//ErpShop shop = this.erpShopService.one(shopId);
		//if( shop != null && secondDomain.equals(shop.getUrl())){
			//return true;
		//}else{
			return this.erpShopService.exists(formMap);
		//}
	}
	/**
	 * 查询商家域名设置的次数
	 * @param shopId
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int setDomainNums(@ErpShop.AnnoShopId String shopId){
		Map formMap = new HashMap();
		formMap.put(LsLog.COL_MEMO, "商家编码:"+shopId+"二级域名设置成功");
		SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap,"a."+LsLog.COL_MEMO,Types.VARCHAR,SqlUtis.EQ));
		return this.lsLogService.count(formMap);
	}
	
	
	/**
	 * 获取配送方式列表
	 * @param shopId
	 * @return
	 */
	public List<ShopDelivery> getDeliveryList(@ErpShop.AnnoShopId String shopId){
		Map formMap = null;
		formMap = new HashMap();
		formMap.put(ErpShop.COL_SHOP_ID, shopId);
		SqlUtis.prepareSql(formMap, " and a.Delivery_Id=b.Delivery_Id "+
				SqlUtis.getSQL(formMap, "b."+ErpShop.COL_SHOP_ID, Types.VARCHAR, SqlUtis.EQ));
		return this.list(this.getStatementName("selectShopDelivery"), formMap);
	}
}
